package com.jumi.microservice.refund;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.jumi.microservice.config.AliPayConfig;
import com.jumi.microservice.dto.PayRefundRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author scw
 * @date 2020/11/5
 */
public class AliPayRefund implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(AliPayRefund.class);
    @Resource
    AliPayConfig aliPayConfig;

    public String aliPayRefundQuery(PayRefundRequest payRefundRequest) {
        String transferQueryResult = "查询异常";
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(payRefundRequest.getOrderSn());
        model.setTradeNo(payRefundRequest.getThirdPartyPaySn());
        model.setOutRequestNo(payRefundRequest.getThirdPartyRefundSn());
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig.getServerUrl(), aliPayConfig.getAppId(), aliPayConfig.getPrivateKey(),
                "json", "utf-8", aliPayConfig.getPublicKey(), "RSA2");
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        request.setBizModel(model);
        try {
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            log.info("refundQuery result>" + response.getBody());
            //返回信息接收
            Map<String, String> returnMap = new HashMap<>();
            String parameters = "{" +
                    "\"out_trade_no\":\"" + model.getOutTradeNo() +
                    "\",\"trade_no\":\"" + model.getTradeNo() +
                    "\",\"out_request_no\":\"" + model.getOutRequestNo()
                    + "\"}";
            returnMap.put("parameters", parameters);
            returnMap.put("code", response.getCode());
            returnMap.put("msg", response.getMsg());
            if (response.isSuccess()) {
                returnMap.put("trade_no", response.getTradeNo());
                returnMap.put("out_trade_no", response.getOutTradeNo());
                returnMap.put("out_request_no", response.getOutRequestNo());
                returnMap.put("refund_reason", response.getRefundReason());
                returnMap.put("total_amount", response.getTotalAmount());
                returnMap.put("refund_amount", response.getRefundAmount());
                returnMap.put("gmt_refund_pay", response.getGmtRefundPay().toString());
                returnMap.put("refund_settlement_id", response.getRefundSettlementId());
                returnMap.put("present_refund_buyer_amount", response.getPresentRefundBuyerAmount());
                returnMap.put("present_refund_discount_amount", response.getPresentRefundDiscountAmount());
                returnMap.put("present_refund_mdiscount_amount", response.getPresentRefundMdiscountAmount());
                returnMap.put("refund_royaltys", response.getRefundRoyaltys().toString());
                returnMap.put("refund_detail_item_list", response.getRefundDetailItemList().toString());
                returnMap.put("error_code", response.getErrorCode());
            } else {
                returnMap.put("sub_code", response.getSubCode());
                returnMap.put("sub_msg", response.getSubMsg());
            }
            transferQueryResult = "查询结果：" + returnMap.toString();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        log.info("查询结果：{}", transferQueryResult);
        return transferQueryResult;
    }
}
